package com.sayice.icemusic.sort.impl;

import com.sayice.icemusic.pojo.Music;
import com.sayice.icemusic.sort.MusicSort;

import java.util.List;

/**
 * 按音乐名相关度排序
 *
 * @author shubing
 */
public class NameSort implements MusicSort {
    @Override
    public void sort(List<Music> musicList, String searchedName) {
        musicList.sort((m1, m2) -> {
            if (m1 == null) {
                if (m2 == null) {
                    return 0;
                }
                return 1;
            }
            if (m2 == null) {
                return -1;
            }
            String name1 = m1.getName();
            String name2 = m2.getName();
            if (name1 == null) {
                if (name2 == null) {
                    return 0;
                }
                return 1;
            }
            if (name2 == null) {
                return -1;
            }
            int index1 = name1.indexOf(searchedName);
            int index2 = name1.indexOf(searchedName);
            if (index1 == -1) {
                if (index2 == -1) {
                    return 0;
                }
                return 1;
            }
            if (index2 == -1) {
                return -1;
            }
            if (index1 != index2) {
                return index1 - index2;
            }
            return name1.length() - name2.length();
        });
    }
}
